package com.kkb.shopcenter.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.UserInfoService;
import com.kkb.shopcenter.common.pojo.UserInfo;
import com.kkb.shopcenter.server.mappers.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> getAllUserInfo() {
        return userInfoMapper.selectAll();
    }

    @Override
    public int addUserInfo(UserInfo userInfo) {
        return userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {
        return userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public int deleteUserInfo(Long id) {
        return userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<UserInfo> getPageUserInfo(int pageNum, int pageSize) {
        //1，设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //2,查询要分页的数据
        List<UserInfo> userInfoList = userInfoMapper.selectAll();
        //3，new一个page，把要查询的数据放进去
        PageInfo<UserInfo> userInfoPageInfo = new PageInfo<>(userInfoList);

        return userInfoPageInfo;
    }

    //根据用户名查询用户信息
    @Override
    public UserInfo findByUsername(String username) {
        //查询条件
        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("userName", username);//判断用户名是否相等
        UserInfo userInfo = userInfoMapper.selectOneByExample(example);
        return userInfo;
    }
}
